﻿///<reference path="../../vswd-ext_2.0.2.js" />
Ext.BLANK_IMAGE_URL = '../../Images/s.gif';


//角色
var roleDs = new Ext.data.Store({
    url: '../../Trans/Base/Role.aspx',
    baseParams: {
        action: 'role_list'
    },
    reader: new Ext.data.JsonReader({
        root: 'rows'
    }, [
        'RoleId',
        'RoleName',
        'RoleMark',
        'RoleDescr',
        'SortIndex',
        'CreateTime',
        'UpdateTime'
    ])
});

Ext.onReady(function() {
    Ext.QuickTips.init();

    var urlArgObj = Ext.urlDecode(window.location.search.substring(1, window.location.search.length));
    var tabHeight = urlArgObj['h'] - 1; //the height of tab panel
    var tabWidth = urlArgObj['w'] - 1; //the width of tab panel
    var pageId = urlArgObj['page_id']; //the function id of the page

    Ext.Ajax.request({
        url: '../../Trans/Base/Lang.aspx',
        params: {
            action: 'lang',
            marks: "'col_role_name','col_role_mark','col_role_descr','col_sort_index','col_create_time','col_update_time','grid_panel_display_msg','grid_panel_empt_msg','max_length_text','blank_text','lbl_role_mark','lbl_role_name_cn','lbl_role_name_en','lbl_role_name_ja','lbl_role_descr','title_tip','title_add','title_edit','title_view','btn_sure','btn_save','btn_close','btn_cancel','msg_select_one_to_eidt','msg_select_one_to_view','msg_select_one_to_del','msg_confirm_to_del','msg_invalid_number'"
        },
        success: function(response, opts) {
            var langObj = Ext.decode(response.responseText);
            langObj = langObj['rows'][0];

            //加载数据
            roleDs.load();

            //角色表单 begin
            //SortIndex
            Ext.form.VTypes['SortIndexVal'] = /^[0-9]\d*$/;
            Ext.form.VTypes['SortIndexText'] = langObj['msg_invalid_number'];
            Ext.form.VTypes['SortIndex'] = function(v) {
                if (v != '') {
                    return Ext.form.VTypes['SortIndexVal'].test(v);
                }
                return true;
            };

            var rolePanel = new Ext.Panel({
                layout: 'column',
                border: false,
                width: 540,
                bodyStyle: 'padding:5px 5px 0;background:transparent;',
                items: [{
                    xtype: 'panel',
                    width: 250,
                    layout: 'form',
                    border: false,
                    bodyStyle: 'padding:5px 5px 0;background:transparent;',
                    items: [{
                        xtype: 'hidden',
                        name: 'RoleId'
                    }, {
                        xtype: 'textfield',
                        width: 120,
                        name: 'RoleMark',
                        fieldLabel: langObj['lbl_role_mark'],
                        maxLength: 10,
                        maxLengthText: langObj['max_length_text'],
                        allowBlank: false,
                        blankText: langObj['blank_text']
                    }, {
                        xtype: 'textfield',
                        width: 120,
                        name: 'RoleName_en',
                        fieldLabel: langObj['lbl_role_name_en'],
                        maxLength: 10,
                        maxLengthText: langObj['max_length_text']
                    }, {
                        xtype: 'textfield',
                        width: 120,
                        name: 'RoleDescr',
                        fieldLabel: langObj['lbl_role_descr'],
                        maxLength: 50,
                        maxLengthText: langObj['max_length_text']
}]
                    }, {
                        xtype: 'panel',
                        width: 250,
                        layout: 'form',
                        border: false,
                        bodyStyle: 'padding:5px 5px 0;background:transparent;',
                        items: [{
                            xtype: 'textfield',
                            width: 120,
                            name: 'RoleName_cn',
                            fieldLabel: langObj['lbl_role_name_cn'],
                            maxLength: 10,
                            maxLengthText: langObj['max_length_text'],
                            allowBlank: false,
                            blankText: langObj['blank_text']
                        }, {
                            xtype: 'textfield',
                            width: 120,
                            name: 'RoleName_ja',
                            fieldLabel: langObj['lbl_role_name_ja'],
                            maxLength: 10,
                            maxLengthText: langObj['max_length_text']
                        }, {
                            xtype: 'textfield',
                            width: 120,
                            name: 'SortIndex',
                            fieldLabel: langObj['col_sort_index'],
                            maxLength: 2,
                            maxLengthText: langObj['max_length_text'],
                            allowBlank: false,
                            blankText: langObj['blank_text'],
                            vtype: 'SortIndex'
}]
}]
                        });

                        var action = null; //操作

                        var roleFormPanel = new Ext.FormPanel({
                            id: 'roleForm',
                            border: false,
                            width: 520,
                            labelWidth: 100,
                            labelAlign: 'right',
                            buttonAlign: 'center',
                            bodyStyle: 'padding:5px 5px 0;background:transparent;',
                            items: [rolePanel],
                            buttons: [{
                                id: 'btn_save',
                                text: langObj['btn_save'],
                                width: 60,
                                handler: function() {
                                    var roleForm = roleFormPanel.getForm();
                                    if (roleForm.isValid()) {
                                        win.hide(null, function() {
                                            var model = '{';
                                            model += "'RoleMark':'" + roleForm.findField('RoleMark').getValue() + "',";
                                            model += "'RoleNameCn':'" + roleForm.findField('RoleName_cn').getValue() + "',";

                                            if (roleForm.findField('RoleId').getValue() != '') {
                                                model += "'RoleId':'" + roleForm.findField('RoleId').getValue() + "',";
                                            }
                                            if (roleForm.findField('RoleName_en').getValue() != '') {
                                                model += "'RoleNameEn':'" + roleForm.findField('RoleName_en').getValue() + "',";
                                            }
                                            if (roleForm.findField('RoleName_ja').getValue() != '') {
                                                model += "'RoleNameJa':'" + roleForm.findField('RoleName_ja').getValue() + "',";
                                            }
                                            model += "'Descr':'" + roleForm.findField('RoleDescr').getValue() + "',";
                                            model += "'SortIndex':'" + roleForm.findField('SortIndex').getValue() + "'}";

                                            Ext.Ajax.request({
                                                url: '../../Trans/Base/Role.aspx',
                                                params: {
                                                    action: action,
                                                    model: model
                                                },
                                                success: function(response, opts) {
                                                    var obj = Ext.decode(response.responseText);

                                                    if (obj['success'] == true) {
                                                        Ext.Msg.show({
                                                            title: langObj['title_tip'],
                                                            msg: obj['msg'],
                                                            buttons: { yes: langObj['btn_sure'] },
                                                            icon: Ext.MessageBox.INFO
                                                        });

                                                        roleDs.reload();
                                                    } else {
                                                        Ext.Msg.show({
                                                            title: langObj['title_tip'],
                                                            msg: obj['msg'],
                                                            buttons: { yes: langObj['btn_sure'] },
                                                            icon: Ext.MessageBox.ERROR
                                                        });
                                                    }
                                                },
                                                failure: function(response, opts) {
                                                    var obj = Ext.decode(response.responseText);
                                                    Ext.Msg.show({
                                                        title: langObj['title_tip'],
                                                        msg: obj['msg'],
                                                        buttons: { yes: langObj['btn_sure'] },
                                                        icon: Ext.MessageBox.ERROR
                                                    });
                                                }
                                            });
                                        }, this);
                                    }
                                }
                            }, {
                                text: langObj['btn_close'],
                                width: 60,
                                handler: function() {
                                    win.hide(null, null, this);
                                }
}]
                            });

                            //显示窗体
                            var win = new Ext.Window({
                                modal: true,
                                resizable: false,
                                closable: false,
                                width: 530,
                                height: 200,
                                items: [roleFormPanel]
                            });

                            //角色表单 end

                            //给用户表单设置初始值
                            function roleFormSetDefault() {
                                var roleForm = roleFormPanel.getForm();

                                roleForm.items.each(function(item, index, lenght) {
                                    item.enable();

                                    if (item.getXType() == 'combo') {
                                        item.setRawValue(langObj['combo_empty_text']);
                                    } else {
                                        item.setRawValue();
                                    }
                                }, this);
                                if (roleFormPanel.getFooterToolbar().findById('btn_save')) {
                                    roleFormPanel.getFooterToolbar().findById('btn_save').enable(); //激活保存按钮
                                }
                            }


                            //刷新
                            function roleRefresh() {
                                roleDs.reload();
                            }

                            //新建
                            function roleAdd() {
                                roleFormSetDefault();
                                action = 'add';
                                win.setTitle(langObj['title_add']);
                                win.show(null, null, this);
                            }

                            //编辑
                            function roleEdit() {
                                if (checkboxSel.hasSelection()) {
                                    var selections = checkboxSel.getSelections();
                                    if (selections.length > 1) {
                                        Ext.Msg.show({
                                            title: langObj['title_tip'],
                                            msg: langObj['msg_select_one_to_eidt'],
                                            buttons: { yes: langObj['btn_sure'] },
                                            icon: Ext.MessageBox.INFO
                                        });
                                    } else {
                                        Ext.Ajax.request({
                                            url: '../../Trans/Base/Role.aspx',
                                            params: {
                                                action: 'model',
                                                sel_role_id: selections[0].data.RoleId
                                            },
                                            success: function(response, opts) {
                                                var obj = Ext.decode(response.responseText);
                                                if (obj['success'] == true) {
                                                    var roleModel = obj['rows'];

                                                    roleFormSetDefault();
                                                    action = 'edit';

                                                    var roleForm = roleFormPanel.getForm();
                                                    roleForm.findField('RoleMark').setValue(roleModel['RoleMark']);
                                                    roleForm.findField('RoleMark').disable();
                                                    roleForm.findField('RoleId').setValue(roleModel['RoleId']);
                                                    roleForm.findField('RoleName_cn').setValue(roleModel['RoleNameCn']);
                                                    roleForm.findField('RoleName_en').setValue(roleModel['RoleNameEn']);
                                                    roleForm.findField('RoleName_ja').setValue(roleModel['RoleNameJa']);
                                                    roleForm.findField('RoleDescr').setValue(roleModel['Descr']);
                                                    roleForm.findField('SortIndex').setValue(roleModel['SortIndex']);

                                                    win.setTitle(langObj['title_edit']);
                                                    win.show(null, null, this);
                                                } else {
                                                    Ext.Msg.show({
                                                        title: langObj['title_tip'],
                                                        msg: obj['msg'],
                                                        buttons: { yes: langObj['btn_sure'] },
                                                        icon: Ext.MessageBox.ERROR
                                                    });
                                                }
                                            },
                                            failure: function(response, opts) {
                                                var obj = Ext.decode(response.responseText);
                                                Ext.Msg.show({
                                                    title: langObj['title_tip'],
                                                    msg: obj['msg'],
                                                    buttons: { yes: langObj['btn_sure'] },
                                                    icon: Ext.MessageBox.ERROR
                                                });
                                            }
                                        });
                                    }
                                } else {
                                    Ext.Msg.show({
                                        title: langObj['title_tip'],
                                        msg: langObj['msg_select_one_to_eidt'],
                                        buttons: { yes: langObj['btn_sure'] },
                                        icon: Ext.MessageBox.INFO
                                    });
                                }
                            }

                            //查看
                            function roleView() {
                                if (checkboxSel.hasSelection()) {
                                    var selections = checkboxSel.getSelections();
                                    if (selections.length > 1) {
                                        Ext.Msg.show({
                                            title: langObj['title_tip'],
                                            msg: langObj['msg_select_one_to_view'],
                                            buttons: { yes: langObj['btn_sure'] },
                                            icon: Ext.MessageBox.INFO
                                        });
                                    } else {
                                        Ext.Ajax.request({
                                            url: '../../Trans/Base/Role.aspx',
                                            params: {
                                                action: 'model',
                                                sel_role_id: selections[0].data.RoleId
                                            },
                                            success: function(response, opts) {
                                                var obj = Ext.decode(response.responseText);
                                                if (obj['success'] == true) {
                                                    var roleModel = obj['rows'];

                                                    roleFormSetDefault();

                                                    var roleForm = roleFormPanel.getForm();

                                                    roleForm.items.each(function(item, index, lenght) {
                                                        item.disable();
                                                    }, this);

                                                    roleForm.findField('RoleMark').setValue(roleModel['RoleMark']);
                                                    roleForm.findField('RoleMark').disable();
                                                    roleForm.findField('RoleId').setValue(roleModel['RoleId']);
                                                    roleForm.findField('RoleName_cn').setValue(roleModel['RoleNameCn']);
                                                    roleForm.findField('RoleName_en').setValue(roleModel['RoleNameEn']);
                                                    roleForm.findField('RoleName_ja').setValue(roleModel['RoleNameJa']);
                                                    roleForm.findField('RoleDescr').setValue(roleModel['Descr']);
                                                    roleForm.findField('SortIndex').setValue(roleModel['SortIndex']);

                                                    roleFormPanel.getFooterToolbar().findById('btn_save').disable(); //禁止保存按钮

                                                    win.setTitle(langObj['title_view']);
                                                    win.show(null, null, this);
                                                } else {
                                                    Ext.Msg.show({
                                                        title: langObj['title_tip'],
                                                        msg: obj['msg'],
                                                        buttons: { yes: langObj['btn_sure'] },
                                                        icon: Ext.MessageBox.ERROR
                                                    });
                                                }
                                            },
                                            failure: function(response, opts) {
                                                var obj = Ext.decode(response.responseText);
                                                Ext.Msg.show({
                                                    title: langObj['title_tip'],
                                                    msg: obj['msg'],
                                                    buttons: { yes: langObj['btn_sure'] },
                                                    icon: Ext.MessageBox.ERROR
                                                });
                                            }
                                        });
                                    }
                                } else {
                                    Ext.Msg.show({
                                        title: langObj['title_tip'],
                                        msg: langObj['msg_select_one_to_view'],
                                        buttons: { yes: langObj['btn_sure'] },
                                        icon: Ext.MessageBox.INFO
                                    });
                                }
                            }

                            //查看历史用户
                            function roleHistory() {
                                window.alert('history');
                            }

                            //搜索
                            function roleSearch() {
                                window.alert('search');
                            }

                            //逻辑删除
                            function roleLogicDel() {
                                if (checkboxSel.hasSelection()) {
                                    Ext.Msg.show({
                                        title: langObj['title_tip'],
                                        msg: langObj['msg_confirm_to_del'],
                                        buttons: { yes: langObj['btn_sure'], no: langObj['btn_cancel'] },
                                        icon: Ext.MessageBox.QUESTION,
                                        fn: function(button, text) {
                                            if (button == 'yes') {
                                                var selections = checkboxSel.getSelections();

                                                var selRoleIds = "";
                                                for (var i = 0; i < selections.length; i++) {
                                                    selRoleIds += selections[i].data.RoleId.toString() + ",";
                                                }
                                                selRoleIds = selRoleIds.substring(0, selRoleIds.length - 1);

                                                Ext.Ajax.request({
                                                    url: '../../Trans/Base/Role.aspx',
                                                    params: {
                                                        action: 'logicdel',
                                                        sel_role_id: selRoleIds
                                                    },
                                                    success: function(response, opts) {
                                                        var obj = Ext.decode(response.responseText);
                                                        if (obj['success'] == true) {
                                                            Ext.Msg.show({
                                                                title: langObj['title_tip'],
                                                                msg: obj['msg'],
                                                                buttons: { yes: langObj['btn_sure'] },
                                                                icon: Ext.MessageBox.INFO
                                                            });

                                                            roleDs.reload();
                                                        } else {
                                                            Ext.Msg.show({
                                                                title: langObj['title_tip'],
                                                                msg: obj['msg'],
                                                                buttons: { yes: langObj['btn_sure'] },
                                                                icon: Ext.MessageBox.ERROR
                                                            });
                                                        }
                                                    },
                                                    failure: function(response, opts) {
                                                        var obj = Ext.decode(response.responseText);
                                                        Ext.Msg.show({
                                                            title: langObj['title_tip'],
                                                            msg: obj['msg'],
                                                            buttons: { yes: langObj['btn_sure'] },
                                                            icon: Ext.MessageBox.ERROR
                                                        });
                                                    }
                                                });
                                            }
                                        }
                                    });

                                } else {
                                    Ext.Msg.show({
                                        title: langObj['title_tip'],
                                        msg: langObj['msg_select_one_to_del'],
                                        buttons: { yes: langObj['btn_sure'] },
                                        icon: Ext.MessageBox.INFO
                                    });
                                }
                            }

                            var checkboxSel = new Ext.grid.CheckboxSelectionModel();

                            var roleGrid = new Ext.grid.GridPanel({
                                id: 'roleGrid',
                                renderTo: document.body,
                                frame: false,
                                height: tabHeight,
                                store: roleDs,
                                loadMask: true,
                                sm: checkboxSel,
                                bbar: new Ext.PagingToolbar({
                                    pageSize: 20,
                                    store: roleDs,
                                    displayInfo: true,
                                    displayMsg: langObj['grid_panel_display_msg'],
                                    emptMsg: langObj['grid_panel_empt_msg']
                                }),
                                columns: [new Ext.grid.RowNumberer(), checkboxSel, {
                                    header: 'RoleId',
                                    dataIndex: 'RoleId',
                                    hidden: true,
                                    sortable: true
                                }, {
                                    header: langObj['col_role_name'],
                                    dataIndex: 'RoleName',
                                    sortable: true,
                                    width: 100
                                }, {
                                    header: langObj['col_role_mark'],
                                    dataIndex: 'RoleMark',
                                    sortable: true,
                                    width: 100
                                }, {
                                    header: langObj['col_role_descr'],
                                    dataIndex: 'RoleDescr',
                                    sortable: true,
                                    width: 100
                                }, {
                                    header: langObj['col_sort_index'],
                                    dataIndex: 'SortIndex',
                                    sortable: true,
                                    width: 50
                                }, {
                                    header: langObj['col_create_time'],
                                    dataIndex: 'CreateTime',
                                    sortable: true,
                                    width: 130
                                }, {
                                    header: langObj['col_update_time'],
                                    dataIndex: 'UpdateTime',
                                    sortable: true,
                                    width: 130
}]
                                });


                                //导出
                                function roleExport() {
                                    window.alert('export');
                                }
                                
                                //权限分配
                                function roleFunction() {
                                    //window.alert('export');
                                    if (checkboxSel.hasSelection()) {
                                        var selections = checkboxSel.getSelections();
                                        if (selections.length > 1) {
                                            Ext.Msg.show({
                                                title: langObj['title_tip'],
                                                msg: langObj['msg_select_one_to_view'],
                                                buttons: { yes: langObj['btn_sure'] },
                                                icon: Ext.MessageBox.INFO
                                            });
                                        } else {
                                            var selroleId = selections[0].data.RoleId.toString();

                                            var roleTreeLoader = new Ext.tree.TreeLoader({
                                                dataUrl: '../../Trans/Base/Role.aspx?action=func_tree&sel_role_id=' + selroleId
                                            });

                                            //roleTreeLoader.load(rootNode, null, this);
                                            var rootNode = new Ext.tree.AsyncTreeNode({
                                                text: langObj['index'],
                                                leaf: false,
                                                expanded: true

                                            });

                                            var treePanel = new Ext.tree.TreePanel({
                                                renderTo: document.body,
                                                height: 250,
                                                loader: roleTreeLoader,
                                                root: rootNode,
                                                useArrows: true,
                                                autoScroll: true,
                                                animate: true,
                                                containerScroll: true,
                                                rootVisible: false
                                            });

                                            //显示窗体
                                            var treeWin = new Ext.Window({
                                                modal: true,
                                                resizable: false,
                                                closable: false,
                                                buttonAlign: 'center',
                                                width: 250,
                                                height: 300,
                                                items: [treePanel],
                                                buttons: [{
                                                    id: 'btn_save',
                                                    text: langObj['btn_save'],
                                                    width: 60,
                                                    handler: function() {
                                                        var strFunIds = [];
                                                        treePanel.expandAll();
                                                        treePanel.expandAll();
                                                        var selNode = treePanel.getChecked();
                                                        for (var funid = 0; funid < selNode.length; funid++) {
                                                            //strFunIds += selNode[funid].id.toString() + ',';
                                                            strFunIds.push(selNode[funid].id);
                                                        }
                                                        //window.alert(strFunIds.join(','));
                                                        Ext.Ajax.request({
                                                            url: '../../Trans/Base/Role.aspx',
                                                            params: {
                                                                action: 'func_set',
                                                                func_role_id: strFunIds.join(','),
                                                                sel_role_id: selroleId
                                                            },
                                                            success: function(response, opts) {
                                                                var obj = Ext.decode(response.responseText);
                                                                if (obj['success'] == true) {
                                                                    Ext.Msg.show({
                                                                        title: langObj['title_tip'],
                                                                        msg: obj['msg'],
                                                                        buttons: { yes: langObj['btn_sure'] },
                                                                        icon: Ext.MessageBox.INFO
                                                                    });

                                                                } else {
                                                                    Ext.Msg.show({
                                                                        title: langObj['title_tip'],
                                                                        msg: obj['msg'],
                                                                        buttons: { yes: langObj['btn_sure'] },
                                                                        icon: Ext.MessageBox.ERROR
                                                                    });
                                                                }
                                                            },
                                                            failure: function(response, opts) {
                                                                var obj = Ext.decode(response.responseText);
                                                                Ext.Msg.show({
                                                                    title: langObj['title_tip'],
                                                                    msg: obj['msg'],
                                                                    buttons: { yes: langObj['btn_sure'] },
                                                                    icon: Ext.MessageBox.ERROR
                                                                });
                                                            }
                                                        });
                                                        treeWin.hide(null, null, this);
                                                    }
                                                }, {
                                                    text: langObj['btn_close'],
                                                    width: 60,
                                                    handler: function() {
                                                        treeWin.hide(null, null, this);
                                                    }
}]
                                                });

                                                treeWin.show(null, null, this);
                                            }
                                        } else {
                                            Ext.Msg.show({
                                                title: langObj['title_tip'],
                                                msg: langObj['msg_select_one_to_view'],
                                                buttons: { yes: langObj['btn_sure'] },
                                                icon: Ext.MessageBox.INFO
                                            });
                                        }
                                    }

                                    //生成功能点
                                    Ext.Ajax.request({
                                        url: '../../Trans/Base/Function.aspx',
                                        params: {
                                            action: 'func_points',
                                            page_id: pageId
                                        },
                                        success: function(response, opts) {
                                            var obj = Ext.decode(response.responseText);

                                            if (obj['success'] == true) {
                                                var funcPointsObj = obj['rows'];
                                                var tb = new Ext.Toolbar();
                                                for (var propName in funcPointsObj) {
                                                    switch (propName) {
                                                        case EF.UI.Const.FuncPoints.REFRESH:
                                                            tb.add({
                                                                text: funcPointsObj[propName],
                                                                width: 60,
                                                                handler: roleRefresh
                                                            });
                                                            break;
                                                        case EF.UI.Const.FuncPoints.ADD:
                                                            tb.add({
                                                                text: funcPointsObj[propName],
                                                                width: 60,
                                                                handler: roleAdd
                                                            });
                                                            break;
                                                        case EF.UI.Const.FuncPoints.EDIT:
                                                            tb.add({
                                                                text: funcPointsObj[propName],
                                                                width: 60,
                                                                handler: roleEdit
                                                            });
                                                            break;
                                                        case EF.UI.Const.FuncPoints.VIEW:
                                                            tb.add({
                                                                text: funcPointsObj[propName],
                                                                width: 60,
                                                                handler: roleView
                                                            });
                                                            break;
                                                        case EF.UI.Const.FuncPoints.SEARCH:
                                                            tb.add({
                                                                text: funcPointsObj[propName],
                                                                width: 60,
                                                                handler: roleSearch
                                                            });
                                                            break;
                                                        case EF.UI.Const.FuncPoints.HISTORY:
                                                            tb.add({
                                                                text: funcPointsObj[propName],
                                                                width: 60,
                                                                handler: roleHistory
                                                            });
                                                            break;
                                                        case EF.UI.Const.FuncPoints.LOGICDEL:
                                                            tb.add({
                                                                text: funcPointsObj[propName],
                                                                width: 60,
                                                                handler: roleLogicDel
                                                            });
                                                            break;
                                                        case EF.UI.Const.FuncPoints.EXPORT:
                                                            tb.add({
                                                                text: funcPointsObj[propName],
                                                                width: 60,
                                                                handler: roleExport
                                                            });
                                                            break;
                                                        case EF.UI.Const.FuncPoints.ROLEFUNCTION:
                                                            tb.add({
                                                                text: funcPointsObj[propName],
                                                                width: 60,
                                                                handler: roleFunction
                                                            });
                                                            break;
                                                    }
                                                }

                                                roleGrid.add(tb);
                                                roleGrid.doLayout();
                                            }
                                        },
                                        failure: function() {

                                        }
                                    });



                                },
                                failure: function() {

                                }
                            });
                        });

